package server.qiang.Thread;

import common.harvin.potocol.Command;
import common.harvin.potocol.Request;
import common.harvin.potocol.Response;
import server.qiang.Process.GetIp;
import server.qiang.Process.P2PConnect;
import server.qiang.Process.Register;
import server.qiang.Process.SendEmail;
import server.qiang.Process.UpdateIp;
import server.qiang.database.IpMap;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.util.concurrent.TimeoutException;


/**
 * 读消息线程和处理方法
 *
 * @author way
 */

public class InputThread extends Thread {
	private Socket socket;// socket对象
	private OutputThread out;// 传递进来的写消息线程，因为我们要给用户回复消息啊
	private OutputThreadMap map;// 写消息线程缓存器
	private IpMap ipmap;
	private ObjectInputStream ois;// 对象输入流
	private boolean isStart = true;// 是否循环读消息

	public InputThread(Socket socket, OutputThread out, OutputThreadMap map, IpMap ipmap) {
		this.socket = socket;
		this.out = out;
		this.map = map;
		this.ipmap = ipmap;
		try {
			ois = new ObjectInputStream(socket.getInputStream());// 实例化对象输入流

		} catch (IOException e) {
			System.out.println("输入流异常");
		}

	}

	public void setStart(boolean isStart) {// 提供接口给外部关闭读消息线程
		this.isStart = isStart;
	}

	@Override
	public void run() {
		try {

			while (isStart) {
				// 读取消息
				
				readMessage();
				//System.out.println("after readmessage.");
			}
			if (ois != null)
				ois.close();
			System.out.println("ois关闭");
			if (socket != null)
				socket.close();
			System.out.println("socket关闭");
			this.out.setStart(false);
		} catch (IOException e) {
			System.out.println("inputstream run 异常");
		}

	}

	/**
	 * 读消息以及处理消息，抛出异常
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	public void readMessage() {
		Object readObject = null;
		try {

			readObject = ois.readObject();// 从流中读取对象


			if (readObject == null) {
				System.out.println("对象空，结束");
				
				return;
			}

		} catch (IOException e1) {
			this.setStart(false);
			
			return;
		
		} catch (ClassNotFoundException e) {
			
			return;
		}

		if (readObject != null && readObject instanceof Request) {
			Request request = (Request) readObject;  // 转换成请求对象
			Response response=new Response();
			
			switch (request.getCommand()) {
				case REGISTER:
					Register register = new Register();
					 response = register.register(request);
					out.setMessage(response);
					break;

				case UPDATE_IP:
					
					map.add(request.getSender(), out);
					UpdateIp updateip = new UpdateIp();
					response = updateip.updateip(socket,request, ipmap);
					System.out.println("update ip");
					out.setMessage(response);
					break;

				case GET_IP:
					GetIp getip = new GetIp();
					response = getip.getip(request,map, ipmap);
					System.out.println("get ip");
					out.setMessage(response);
					
					
					
					/*P2PConnect p2pconnect=new P2PConnect();
					response=p2pconnect.p2pconnect(request, ipmap);
					OutputThread outo = map.getById(request.getReceiver());	
					//TODO
					if(outo!=null)
					{
					outo.setMessage(response);
					//System.out.println("p2pconnect send ");
					}else
					{
						//System.out.println("p2p,receiver not exist ");
					}*/
					break;

				case SEND_EMAIL:
					SendEmail sendemail = new SendEmail();
					 response= sendemail.sendemail(request);
					System.out.println("send email");
					out.setMessage(response);
					break;

				case SEND_MESSAGE:
					System.out.println("command:" + request.getCommand());
					System.out.println("sender:" + request.getSender());
					System.out.println("receiver:" + request.getReceiver());
					System.out.println("data:" + request.getData("message"));
					System.out.println("send_Date:" + request.getSend_Date());
					
					
					
					response.setFor_command(request.getCommand());
					response.setSender(request.getSender());
					response.setReceiver(request.getReceiver());
					response.setSend_Date(request.getSend_Date());
					response.setResult(true);
					response.addData("message", request.getData("message"));
					OutputThread out = map.getById(request.getReceiver());
					if(out!=null)
					{
					out.setMessage(response);
					System.out.println("转发信息 成功");
					}
					else
					{
						System.out.println("转发信息 失败");
					}
					
					break;
				case CONFIRM:
					System.out.println("command:" + request.getCommand());
					System.out.println("sender:" + request.getSender());
					System.out.println("receiver:" + request.getReceiver());
					System.out.println("data:" + request.getData("message"));
					System.out.println("send_Date:" + request.getSend_Date());
					
					
					
					response.setFor_command(request.getCommand());
					response.setSender(request.getSender());
					response.setReceiver(request.getReceiver());
					response.setSend_Date(request.getSend_Date());
					response.setResult(true);
					//response.addData("message", request.getData("message"));
					OutputThread out0 = map.getById(request.getReceiver());
					if(out0!=null)
					{
					out0.setMessage(response);
					System.out.println("转发确认信息 成功");
					}
					else
					{
						System.out.println("转发确认失败");
					}
					
					break;	
					
					

				default:
					System.out.println("nothing match.");
					break;
			}

		}else{
			
		}
	}
}
